Download YouTube video through .net code

27 06 2008

Using following methods you can download youtube videos directly from your browser without using any software.

1. Retrieve the id video from the link (the code after ‘v=’)
example: http://www.youtube.com/watch?v=NegsNQy1ZoU => NegsNQy1ZoU
2. In your favourite browser go to http://youtube.com/v/{ID video}
example: http://youtube.com/v/NegsNQy1ZoU
3. Link in the address bar will change. Replace ’swf/l.swf’ with ‘get_video’ and press Enter
4. Save your file

Just experienc it in your browser.

Do It Progrmatically:

Now we will do it programaticaly using asp.net. Just follow followin steps

1. Create a page named as DownloadUTube.aspx

<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td>YouTube URL: <asp:TextBox ID=”txtUrl” runat=”server”></asp:TextBox></td>
<td><asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” /></td>
</tr>
<tr>
<td align=”center” colspan=”2″>Example: http://www.youtube.com/watch?v=NegsNQy1ZoU </td>
</tr>
<tr>
<td colspan=”2″><asp:Literal ID=”litResult” runat=”server”></asp:Literal></td>
</tr>
</table>
</div>
</form>
</body>

You have successfully completed 1st step now proceed to 2nd one.

Step 2:

Now in your cSharp file you have to go as follow.

// First of all we will change our utube link to our desired format.

string URL = this.txtUrl.Text;
string VideoId = URL.Substring(URL.IndexOf(‘=’)+1);
URL = @”http://youtube.com/v/&#8221; + VideoId;

// Now we will send request to server which will create new link.
WebRequest MyRequest = HttpWebRequest.Create(URL);
WebResponse MyResponse = MyRequest.GetResponse();

// we get newly create link into string RealURL
string RealURL = MyResponse.ResponseUri.ToString();

// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RealURL);

// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(“utf-8″);

// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);

// Now we will write whole stream to specified. It is the path within your website.
StreamWriter writer = new StreamWriter(Server.MapPath(@”Contents\Videos\YourVideoName.flv”), true);
writer.Write(readStream.ReadToEnd());
writer.Close();

Just go on specified path and u will see the video.

Final saying, if you have any problem about this. write to me. I will try my best to solve it.

Muhammad Tahir Rauf


Actions

Information

7 responses

27 06 2008
binaryday

Good one.

27 06 2008
ipod chargers

The youtube video tip is useful! Thanks!

17 07 2008
Bansh Patel

Hi Tahir,

Thanks a lot buddy. Realy thats helfull for me.
Thanks again.

Bansh Patel
System Analyst
banshpatel@yahoo.co.in

4 09 2008
Pawan Kumar Singh

Hey Bro you have created a good program………..also there is another way to minimize the code i.e.

string videoID = txtPath.Text.Substring(txtPath.Text.IndexOf(‘=’) + 1);
WebRequest wReq = HttpWebRequest.Create(@”http://youtube.com/v/” + videoID);
WebResponse wRes = wReq.GetResponse();
string originalVdo = wRes.ResponseUri.OriginalString.Replace(“swf/l.swf”, “get_video”);
WebClient wc = new WebClient();
wc.DownloadFile(originalVdo, @”c:\fileName.flv”);

ur work is dam good…………….

7 10 2008
Salvio

a good way I use to download videos from youtube is in this site

http://www.freefileconvert.com

you can download youtube and metacafe videos, and also convert files format

It is very quick and easy, worked for me

1 02 2009
ishrat jahan

OK

Leave a comment